const Service = require('../../../core/service/ApiService');

class ServiceDao extends Service {

    //获取所有服务项目
    async getAllServiceItem() {
        return this.model.ServiceItem.findAll({
            where: {
                status: this.constant.SVC_ITEM_STATUS.ENABLE
            },
            attributes: ['svc_item_id', 'name'],
            raw: true
        })
    }

    //创建服务记录
    async addSvcRecord(staff_id, aged_id, start_time, status) {
        return this.model.ServiceRecord.create({
            svc_staff_id: staff_id,
            aged_id: aged_id,
            start_time: start_time,
            score: 0,
            status: status
        })
    }

    //增加月服务次数
    async increaseCurMonthSvcCnt(staff_id, cnt) {
        await this.model.ServiceStaff.update({
            cur_month_svc_cnt: this.model.literal(`cur_month_svc_cnt + ${cnt}`)
        }, {
            where: {
                svc_staff_id: staff_id
            }
        })
    }

    //更新老人最后一次服务时间，以及服务状态
    async updateAgedSvcStatusAndLastSvcTime(aged_id, last_svc_time, status) {
        await this.model.Aged.update({
            svc_status: this.constant.AGED_SVC_STATUS.IN_SERVICE,
            last_svc_time: last_svc_time
        }, {
            where: {
                aged_id: aged_id
            }
        })
    }

    //增加月异常服务次数
    async increaseCurMonthSvcAbnormalCnt(staff_id, cnt) {
        await this.model.ServiceStaff.update({
            cur_month_svc_abn_cnt: this.model.literal(`cur_month_svc_abn_cnt + ${cnt}`)
        }, {
            where: {
                svc_staff_id: staff_id
            }
        })
    }

    //根据服务人员id和老人id查找是否存在记录
    async getInServiceRecord(staff_id, aged_id) {
        return this.model.ServiceRecord.findOne({
            where: {
                svc_staff_id: staff_id,
                aged_id: aged_id,
                status: this.constant.SVC_STATUS.SIGNED
            },
            attributes: ['id', 'start_time'],
            order: [['start_time', 'DESC']],
            raw: true
        })
    }

    //更新服务记录
    async updateSvcRecord(record) {
        return this.model.ServiceRecord.update({
            end_time: record.end_time,
            score: record.score,
            text_evalu: record.text_evalu,
            voice_evalu: record.voice_evalu,
            svc_item_list: record.svc_item_list.join(','),
            status: record.status
        }, {
            where: {
                id: record.id
            }
        })
    }

    //更新老人服务状态
    async updateAgedSvcStatus(service) {
        await this.model.Aged.update({
            svc_status: service.status
        }, {
            where: {
                aged_id: service.aged_id,
                status: this.constant.AGED_STATUS.ENABLE,
                svc_status: this.constant.AGED_SVC_STATUS.IN_SERVICE
            }
        })
    }

    //计算平均评价
    async getScoreByCompute(staff_id) {
        const sql = `select avg(score) as avg_score from service_record where svc_staff_id = ${staff_id} and status = ${this.constant.SVC_STATUS.EXCEPTION}`;
        const avg_score = await this.model.query(sql, {type: this.model.QueryTypes.SELECT});
        return parseFloat(avg_score[0].avg_score);
    }

    //更新staff表的score
    async updateScore(staff_id, score) {
        await this.model.ServiceStaff.update({
            score: score
        }, {
            where: {
                svc_staff_id: staff_id
            }
        })
    }


    //获取服务记录（通过日期区间，页码）
    async getSvcRecordByDate(staff_id, begin, end, pn, ps) {
        return this.model.ServiceRecord.findAll({
            where: {
                svc_staff_id: staff_id,
                status: {$in: [this.constant.SVC_STATUS.SIGNED, this.constant.SVC_STATUS.EXCEPTION]},
                start_time: {
                    $gt: begin,
                    $lt: end
                }
            },
            attributes: ['id', 'start_time'],
            offset: (pn - 1) * ps,
            limit: ps,
            raw: true
        })
    }

    //获取服务次数
    async getSvcCount(staff_id) {
        return this.model.ServiceRecord.count({
            where: {
                svc_staff_id: staff_id,
                status: this.constant.SVC_STATUS.EXCEPTION
            }
        });
    }


}

module.exports = ServiceDao;
